//
//  BZDatePanel.m
//  BZBaseWidget
//
//  Created by 郭艺伟 on 2017/10/12.
//  Copyright © 2017年 quheng. All rights reserved.
//

#import "BZDatePanel.h"
#import "BZDatePicker.h"
#import <Masonry/Masonry.h>

#define BZDatePanelHeight (264+44)

@interface BZDatePanel()
/**
 *  点击是否返回
 */
@property (assign, nonatomic) BOOL isTapReturn;

@end

@implementation BZDatePanel{
    UIView *backgroundView;
    UIView *contentView;
    void (^pickUpDate)(NSDate *date);
    void (^pickUpDates)(NSDate *dateFirst,NSDate *dateLast);
    BZDatePicker *datePickerFirst;
    BZDatePicker *datePickerLast;
}

/**
 *  单时间选择调用接口
 *
 *  @param view
 *  @param title
 *  @param defaultDate
 *  @param mode
 *  @param pick
 */
+ (void)showInView:(UIView *)view
         titleText:(NSString *)title
       defaultDate:(NSDate *)defaultDate
              mode:(BZDatePickerMode) mode
        pickUpDate:(void (^)(NSDate *date))pick {
    
    BZDatePanel *pickerView = [[BZDatePanel alloc] initWithFrame:view.bounds
                                                         titleText:title
                                                              mode:mode
                                                       defaultDate:defaultDate
                                                       minimumDate:nil
                                                       maximumDate:nil
                                                             isTap:NO
                                                        pickUpDate:pick];
    //pickerView.pickUpDate = pick;
    [view addSubview:pickerView];
    [pickerView show];
}

/**
 *  单时间选择调用接口点击遮罩返回
 *
 *  @param view
 *  @param title
 *  @param defaultDate
 *  @param mode
 *  @param pick
 */
+ (void)showInView:(UIView *)view
         titleText:(NSString *)title
       defaultDate:(NSDate *)defaultDate
              mode:(BZDatePickerMode) mode
   isTapMaskReturn:(BOOL)isReturn
        pickUpDate:(void (^)(NSDate *date))pick {
    
    //    NSDate *maximumDate = [NSDate dateForToday];
    //    NSDate *minimumDate = [maximumDate dateByAddingDays:-3];
    
    BZDatePanel *pickerView = [[BZDatePanel alloc] initWithFrame:view.bounds
                                                         titleText:title
                                                              mode:mode
                                                       defaultDate:defaultDate
                                                       minimumDate:nil
                                                       maximumDate:nil
                                                             isTap:isReturn
                                                        pickUpDate:pick];
    //pickerView.pickUpDate = pick;
    [view addSubview:pickerView];
    [pickerView show];
}

/**
 *  单时间选择调用接口点击遮罩返回
 *
 *  @param view
 *  @param title
 *  @param defaultDate
 *  @param minimumDate
 *  @param maximumDate
 *  @param mode
 *  @param pick
 */
+ (void)showInView:(UIView *)view
         titleText:(NSString *)title
       defaultDate:(NSDate *)defaultDate
       minimumDate:(NSDate *)minimumDate
       maximumDate:(NSDate *)maximumDate
              mode:(BZDatePickerMode) mode
   isTapMaskReturn:(BOOL)isReturn
        pickUpDate:(void (^)(NSDate *date))pick {
    
    BZDatePanel *pickerView = [[BZDatePanel alloc] initWithFrame:view.bounds
                                                         titleText:title
                                                              mode:mode
                                                       defaultDate:defaultDate
                                                       minimumDate:(NSDate *)minimumDate
                                                       maximumDate:(NSDate *)maximumDate
                                                             isTap:isReturn
                                                        pickUpDate:pick];
    [view addSubview:pickerView];
    [pickerView show];
}

/**
 *  初始化单时间选择
 *
 *  @param frame
 *  @param title
 *  @param mode
 *  @param defaultDate
 *  @param minimumDate
 *  @param maximumDate
 *
 *  @return
 */
- (id)initWithFrame:(CGRect)frame titleText:(NSString *)title
               mode:(BZDatePickerMode)mode
        defaultDate:(NSDate *)defaultDate
        minimumDate:(NSDate *)minimumDate
        maximumDate:(NSDate *)maximumDate
              isTap:(BOOL)istap
         pickUpDate:(void(^)(NSDate *))handler {
    
    if (self = [super initWithFrame:frame]) {
        // Initialization code
        self.isTapMaskReturn = istap;
        pickUpDate = handler;
        [self initializationWithtitleText:title
                                     mode:mode
                              defaultDate:defaultDate
                              minimumDate:(NSDate *)minimumDate
                              maximumDate:(NSDate *)maximumDate];
    }
    return self;
}

/**
 *  初始化单时间选择
 *
 *  @param title
 *  @param mode
 *  @param defaultDate
 *  @param minimumDate
 *  @param maximumDate
 */
- (void)initializationWithtitleText:(NSString *)title
                               mode:(BZDatePickerMode)mode
                        defaultDate:(NSDate *)defaultDate
                        minimumDate:(NSDate *)minimumDate
                        maximumDate:(NSDate *)maximumDate {
    
    //添加遮照层
    CGSize screeSize = [UIScreen mainScreen].bounds.size;
    backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screeSize.width, screeSize.height)];
    backgroundView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.7f];
    [self addSubview:backgroundView];
    
    if (self.isTapMaskReturn) {
        UITapGestureRecognizer *returntap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelDatePick)];
        [backgroundView addGestureRecognizer:returntap];
    }
    //白底
    contentView = [[UIView alloc] initWithFrame:CGRectMake(0, screeSize.height - BZDatePanelHeight, screeSize.width, BZDatePanelHeight)];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.alpha = 1.0;
    [self addSubview:contentView];
    
    UIView *toolBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screeSize.width, 44)];
    toolBar.backgroundColor = [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
    
    UIButton *leftBt = [[UIButton alloc]initWithFrame:CGRectMake(10, 5, 50, 33)];
    [leftBt setTitle:@"取消" forState:UIControlStateNormal];
    [leftBt setTitleColor:[UIColor colorWithRed:90.0/255.0 green:186.0/255.0 blue:246.0/255.0 alpha:1] forState:UIControlStateNormal];
    [leftBt setTitleColor:[UIColor colorWithRed:65.0/255.0 green:158.0/255.0 blue:251.0/255.0 alpha:1] forState:UIControlStateHighlighted];
    [leftBt addTarget:self action:@selector(cancelDatePick) forControlEvents:UIControlEventTouchUpInside];
    [toolBar addSubview:leftBt];
    
    UIButton *rightBt = [[UIButton alloc]initWithFrame:CGRectMake(screeSize.width - 10 - 50 , 5, 50, 33)];
    [rightBt setTitleColor:[UIColor colorWithRed:90.0/255.0 green:186.0/255.0 blue:246.0/255.0 alpha:1] forState:UIControlStateNormal];
    [rightBt setTitleColor:[UIColor colorWithRed:65.0/255.0 green:158.0/255.0 blue:251.0/255.0 alpha:1] forState:UIControlStateHighlighted];
    [rightBt addTarget:self action:@selector(confirmDatePick) forControlEvents:UIControlEventTouchUpInside];
    [rightBt setTitle:@"确定" forState:UIControlStateNormal];
    [toolBar addSubview:rightBt];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screeSize.width, 44)];
    titleLabel.text = title;
    [titleLabel setTextColor:[UIColor grayColor]];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.center = toolBar.center;
    [toolBar addSubview:titleLabel];
    
    [contentView addSubview:toolBar];
    
    //时间滚动
    datePickerFirst = [[BZDatePicker alloc] initWithFrame:CGRectMake(0, 44, screeSize.width, BZDatePanelHeight-44) mode:mode date:defaultDate];
    datePickerFirst.minimumDate = minimumDate;
    datePickerFirst.maximumDate = maximumDate;
    [contentView addSubview:datePickerFirst];
}

#pragma mark event

/**
 *  显示时间
 */
- (void)show {
    
    [self layoutIfNeeded];
    
    self.alpha = 0;
    contentView.transform = CGAffineTransformMakeTranslation(0,contentView.frame.size.height);
    [UIView animateWithDuration:.3f animations:^{
        //面板显示
        self.alpha = 1;
        //回到原来位置
        contentView.transform = CGAffineTransformIdentity;
    }];
}

/**
 *  隐藏时间
 */
- (void)dismiss {
    
    [UIView animateWithDuration:.3f animations:^{
        //隐藏掉
        self.alpha = 0;
        //偏离原来位置
        contentView.transform = CGAffineTransformMakeTranslation(0,contentView.frame.size.height);
    } completion:^(BOOL finished) {
        //删除视图
        [self removeFromSuperview];
    }];
}

/**
 *  取消
 */
- (void)cancelDatePick {
    
    [self dismiss];
}

/**
 *  确定
 */
- (void)confirmDatePick {
    
    if (pickUpDates) {
        //        if ([datePickerLast.date timeIntervalSinceDate:datePickerFirst.date]< 0) {
        //            NSDate *temp =datePickerFirst.date;
        //            datePickerFirst.date = datePickerLast.date;
        //            datePickerLast.date = temp;
        //        }
        pickUpDates(datePickerFirst.date,datePickerLast.date);
    }
    
    if (pickUpDate) pickUpDate(datePickerFirst.date);
    [self dismiss];
}

/**
 *  开始时间与结束时间选择调用接口
 *
 *  @param view
 *  @param titleFirst
 *  @param titleLast
 *  @param defaultFirstDate
 *  @param defaultLastDate
 *  @param mode
 *  @param pick
 */
+ (void)showInView:(UIView *)view
    titleFirstText:(NSString *)titleFirst
     titleLastText:(NSString *)titleLast
  defaultFirstDate:(NSDate *)defaultFirstDate
   defaultLastDate:(NSDate *)defaultLastDate
              mode:(BZDatePickerMode) mode
        pickUpDate:(void (^)(NSDate *dateFirst,NSDate *dateLast))pick {
    
    BZDatePanel *pickerView = [[BZDatePanel alloc] initWithFrame:view.frame
                                                    titleFirstText:titleFirst
                                                     titleLastText:titleLast
                                                              mode:mode
                                                  defaultFirstDate:defaultFirstDate
                                                   defaultLastDate:defaultLastDate
                                                             isTap:NO
                                                        pickUpDate:pick
                                ];
    //pickerView.pickUpDates = pick;
    [view addSubview:pickerView];
    [pickerView show];
}


/**
 *  开始时间与结束时间选择调用接口点击遮罩返回
 *
 *  @param view
 *  @param titleFirst
 *  @param titleLast
 *  @param defaultFirstDate
 *  @param defaultLastDate
 *  @param mode
 *  @param pick
 */
+ (void)showInView:(UIView *)view
    titleFirstText:(NSString *)titleFirst
     titleLastText:(NSString *)titleLast
  defaultFirstDate:(NSDate *)defaultFirstDate
   defaultLastDate:(NSDate *)defaultLastDate
              mode:(BZDatePickerMode) mode
   isTapMaskReturn:(BOOL)isReturn
        pickUpDate:(void (^)(NSDate *dateFirst,NSDate *dateLast))pick {
    
    BZDatePanel *pickerView = [[BZDatePanel alloc] initWithFrame:view.frame
                                                    titleFirstText:titleFirst
                                                     titleLastText:titleLast
                                                              mode:mode
                                                  defaultFirstDate:defaultFirstDate
                                                   defaultLastDate:defaultLastDate
                                                             isTap:isReturn
                                                        pickUpDate:pick
                                ];
    //pickerView.pickUpDates = pick;
    [view addSubview:pickerView];
    [pickerView show];
}

/**
 *  初始化开始与结束时间面板
 *
 *  @param frame
 *  @param titleFirst
 *  @param titleLast
 *  @param mode
 *  @param defaultFirstDate
 *  @param defaultLastDate
 *
 *  @return
 */
- (id)initWithFrame:(CGRect)frame
     titleFirstText:(NSString *)titleFirst
      titleLastText:(NSString *)titleLast
               mode:(BZDatePickerMode) mode
   defaultFirstDate:(NSDate *)defaultFirstDate
    defaultLastDate:(NSDate *)defaultLastDate
              isTap:(BOOL)istap
         pickUpDate:(void (^)(NSDate *dateFirst,NSDate *dateLast))handler {
    
    if (self = [super initWithFrame:frame]) {
        self.isTapMaskReturn = istap;
        pickUpDates = handler;
        [self initializationWithtitleFirstText:titleFirst
                                 titleLastText:titleLast
                                          mode:mode
                              defaultFirstDate:defaultFirstDate
                               defaultLastDate:defaultLastDate];
    }
    return self;
}

/**
 *  初始化开始与结束时间面板
 *
 *  @param titleFirst
 *  @param titleLast
 *  @param mode
 *  @param defaultFirstDate
 *  @param defaultLastDate
 */
- (void)initializationWithtitleFirstText:(NSString *)titleFirst
                           titleLastText:(NSString *)titleLast
                                    mode:(BZDatePickerMode) mode
                        defaultFirstDate:(NSDate *)defaultFirstDate
                         defaultLastDate:(NSDate *)defaultLastDate {
    
    CGSize screeSize = [UIScreen mainScreen].bounds.size;
    backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screeSize.width, screeSize.height)];
    backgroundView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.7f];
    [self addSubview:backgroundView];
    
    if (self.isTapMaskReturn) {
        UITapGestureRecognizer *returntap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelDatePick)];
        [backgroundView addGestureRecognizer:returntap];
    }
    
    contentView = [[UIView alloc] initWithFrame:CGRectMake(0, screeSize.height - 386, screeSize.width, 385)];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.alpha = 1.0;
    [self addSubview:contentView];
    
    /*
     UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, screeSize.width, 44)];
     toolBar.barStyle = UIBarStyleDefault;
     UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"取消"
     style:UIBarButtonItemStyleDone
     target:self
     action:@selector(cancelDatePick)];
     
     UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"确定"
     style:UIBarButtonItemStyleDone
     target:self
     action:@selector(confirmDatePick)];
     
     UIBarButtonItem * spaceBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
     target:nil
     action:nil];
     UIBarButtonItem * titleBtn = [[UIBarButtonItem alloc] initWithTitle:titleFirst
     style:UIBarButtonItemStylePlain
     target:nil
     action:nil];
     [titleBtn setTintColor:[UIColor grayColor]];
     
     
     [toolBar setItems:@[leftButton,spaceBtn,titleBtn,spaceBtn,rightButton] animated:YES];
     */
    
    UIView *toolBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screeSize.width, 44)];
    toolBar.backgroundColor = [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
    
    UIButton *leftBt = [[UIButton alloc]initWithFrame:CGRectMake(10, 5, 50, 33)];
    [leftBt setTitle:@"取消" forState:UIControlStateNormal];
    [leftBt setTitleColor:[UIColor colorWithRed:90.0/255.0 green:186.0/255.0 blue:246.0/255.0 alpha:1] forState:UIControlStateNormal];
    [leftBt addTarget:self action:@selector(cancelDatePick) forControlEvents:UIControlEventTouchUpInside];
    [toolBar addSubview:leftBt];
    
    UIButton *rightBt = [[UIButton alloc]initWithFrame:CGRectMake(screeSize.width - 10 - 50 , 5, 50, 33)];
    [rightBt setTitleColor:[UIColor colorWithRed:90.0/255.0 green:186.0/255.0 blue:246.0/255.0 alpha:1] forState:UIControlStateNormal];
    [rightBt addTarget:self action:@selector(confirmDatePick) forControlEvents:UIControlEventTouchUpInside];
    [rightBt setTitle:@"确定" forState:UIControlStateNormal];
    [toolBar addSubview:rightBt];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screeSize.width, 44)];
    titleLabel.text = titleFirst;
    [titleLabel setTextColor:[UIColor grayColor]];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.center = toolBar.center;
    [toolBar addSubview:titleLabel];
    
    [contentView addSubview:toolBar];
    [toolBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentView.mas_top).with.offset(0);
        make.leading.equalTo(contentView.mas_leading).with.offset(0);
        make.trailing.equalTo(contentView.mas_trailing).with.offset(0);
        make.height.equalTo(@(44));
    }];
    
    datePickerFirst = [[BZDatePicker alloc]initWithFrame:CGRectMake(0, 44, screeSize.width, 130) mode:mode date:defaultFirstDate];
    if (defaultFirstDate) {
        datePickerFirst.date = defaultFirstDate;
    }
    
    [contentView addSubview:datePickerFirst];
    [datePickerFirst mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(toolBar.mas_bottom).with.offset(0);
        make.leading.equalTo(contentView.mas_leading).with.offset(0);
        make.trailing.equalTo(contentView.mas_trailing).with.offset(0);
        make.height.equalTo(@(130));
    }];
    
    UILabel *titleLastLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screeSize.width, 44)];
    titleLastLabel.text = titleLast;
    [titleLastLabel setTextColor:[UIColor grayColor]];
    titleLastLabel.textAlignment = NSTextAlignmentCenter;
    [contentView addSubview:titleLastLabel];
    [titleLastLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(datePickerFirst.mas_bottom).with.offset(20);
        make.leading.equalTo(contentView.mas_leading).with.offset(0);
        make.trailing.equalTo(contentView.mas_trailing).with.offset(0);
        make.height.equalTo(@(44));
    }];
    
    datePickerLast = [[BZDatePicker alloc]initWithFrame:CGRectMake(0, 44, screeSize.width, 130) mode:mode date:defaultLastDate];
    if (defaultLastDate) {
        datePickerLast.date = defaultLastDate;
    }
    
    [contentView addSubview:datePickerLast];
    [datePickerLast mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLastLabel.mas_bottom).with.offset(-10);
        make.leading.equalTo(contentView.mas_leading).with.offset(0);
        make.trailing.equalTo(contentView.mas_trailing).with.offset(0);
        make.height.equalTo(@(130));
    }];
}


@end
